<?php
$inc_path = "../";
require($inc_path."global.php");
uses("member");
$member = new Members();
if (isset($_POST['get_password']) && !empty($_POST['login_name'])) {
	$login_name = trim($_POST['login_name']);
	$useremail = trim($_POST['useremail']);
	$ifexists = $member->checkUserExist($login_name);
	$email_exists = $g_db->GetOne("select username from ".$member->getTable()." where email='".$useremail."'");
	if ($ifexists  && !empty($email_exists)) {
		$memberinfo = $g_db->GetRow("select id,email,firstname,lastname from ".$tb_prefix."members where username='$login_name'");
		if(ua_checkEmail($memberinfo['email'])){
			require_once(INC_PATH."phpmailer/class.phpmailer.php");
			$mail = new PHPMailer();
			$new_passwd = getRadomStr(6);
			$new_passwd_md5 = md5($new_passwd);
			$body = $mail->getFile($inc_path.'templates/'.$theme_name.'/element.getpass.html');
			$body             = str_replace("[field:username]", $login_name, $body);
			$body             = str_replace("[field:newpassword]", $new_passwd, $body);
			$body             = str_replace("[field:sitename]", $_SETTINGS['sitename'], $body);
			$body             = str_replace("[field:sitetitle]", $_SETTINGS['sitetitle'], $body);
			$body             = str_replace("[field:fulldate]", date("Y年m月d日"), $body);
			$body             = str_replace("[field:siteurl]", URL, $body);
			$sended = uaMailTo($memberinfo['email'], $login_name, $login_name.",这是您在".$_SETTINGS['sitename']."的新密码", $body);
			if(!$sended)
			{
			   alert("邮件发送不成功,错误信息: " . $mail->ErrorInfo, true);
			}else{
				$sql = "update ".$member->getTable()." set userpass='".$new_passwd_md5."' where id=".$memberinfo['id']." and status='1'";
				$g_db->Execute($sql);
				alert(urlencode(lgg("get_passwd_success")), true);
			}
		}else {
			setvar("ERRORS", lgg('get_passwd_false')."！");
		}
	}else {
		setvar("ERRORS",lgg('member_not_exists'));
	}
}
template($theme_name."/user_getpass");
?>